﻿namespace Babylon.Toolbox
{
    static partial class EffectCacheSystem
    {
        static readonly Effect[] vertexRegistersOwners = new Effect[256];
        static readonly Effect[] pixelRegistersOwners = new Effect[256];

        internal static bool DoIOwnVertexRegisters(Effect effect, int startIndex, int count)
        {
            return DoIOwnRegisters(effect, startIndex, count, vertexRegistersOwners);
        }

        internal static bool DoIOwnPixelRegisters(Effect effect, int startIndex, int count)
        {
            return DoIOwnRegisters(effect, startIndex, count, pixelRegistersOwners);
        }

        internal static void TakeOwnershipOfVertexRegisters(Effect effect, int startIndex, int count)
        {
            TakeOwnershipOfRegisters(effect, startIndex, count, vertexRegistersOwners);
        }

        internal static void TakeOwnershipOfPixelRegisters(Effect effect, int startIndex, int count)
        {
            TakeOwnershipOfRegisters(effect, startIndex, count, pixelRegistersOwners);
        }

        static bool DoIOwnRegisters(Effect effect, int startIndex, int count, Effect[] registersOwners)
        {
            for (int index = startIndex; index < startIndex + count; index++)
            {
                if (registersOwners[index] != effect)
                    return false;
            }

            return true;
        }

        static void TakeOwnershipOfRegisters(Effect effect, int startIndex, int count, Effect[] registersOwners)
        {
            for (int index = startIndex; index < startIndex + count; index++)
            {
                registersOwners[index] = effect;
            }
        }
    }
}
